<script>
  function createClient() {
    const client = new WebSocket(`ws://${location.host}`);
    client.addEventListener('open', () => console.log('opened'));
    client.addEventListener('close', () => console.log('closed'));
    return client;
  }

  function createAndDestroyClientAfterDelay(delay = 50) {
    const client = createClient();
    return new Promise(resolve => {
      setTimeout(() => {
        client.close();
        resolve();
      }, delay);
    });
  }

  async function main() {
    await createAndDestroyClientAfterDelay();
    await createAndDestroyClientAfterDelay();
    await createAndDestroyClientAfterDelay();
    await createAndDestroyClientAfterDelay();
    await fetch('fetch-request.js');
  }

  main().then(() => console.log('done!'));
</script>
